package com.benben.commoncore.utils;

import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;

import androidx.appcompat.app.AlertDialog;

import com.benben.commoncore.R;

/**
 * Created by zhf on 2018/10/31.
 */

public class ProgressUtils {

    public static AlertDialog builder;

    public static void showDialog(Activity activity) {
        View view = LayoutInflater.from(activity).inflate(R.layout.pull_to_refresh_clife, null);
        if (builder == null) {
            builder = new AlertDialog.Builder(activity, R.style.iphone_progress_dialog).create();
        }
        ImageView imageView = (ImageView) view.findViewById(R.id.lodimg);
        imageView.setImageResource(R.drawable.icon_loading_animation);
        AnimationDrawable mAnimationDrawable = (AnimationDrawable) imageView.getDrawable();
        mAnimationDrawable.start();
        builder.show();
        builder.getWindow().setBackgroundDrawableResource(R.color.transparent);
        builder.getWindow().setContentView(view);
        builder.setCanceledOnTouchOutside(false);
        Window dialogWindow = builder.getWindow();
        dialogWindow.setGravity(Gravity.CENTER);
        WindowManager m = activity.getWindowManager();
        Display d = m.getDefaultDisplay();
        WindowManager.LayoutParams p = dialogWindow.getAttributes();
        p.width = (int) (d.getWidth() * 0.8);
        dialogWindow.setAttributes(p);
    }

    public static void dissDialog() {
        if (builder != null) {
            builder.dismiss();
            builder = null;
        }
    }

}
